//
//  MCChangeStoreViewController.swift
//  StoreManage
//
//  Created by MC on 2018/1/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCClerkManageViewController: MCViewController {
    
    private var pageNumber = 1
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        pageNumber = 1
        sendNetworking()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }

    
    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()

}

extension MCClerkManageViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArrayM.count
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCClerkManageCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        cell.model = self.dataArrayM[indexPath.row] as! MCClerkManageModel
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.dataArrayM[indexPath.row] as! MCClerkManageModel
        
        let vc = MCClerkDetailViewController()
        vc.model = model
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
}

//MARK: 通知回调，闭包回调，点击事件
extension MCClerkManageViewController {
    @objc func addButtonClicked() {
        let vc = MCClerkDetailViewController()
        vc.isNew = true
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
}


//MARK: 网络请求
extension MCClerkManageViewController {
    func sendNetworking() {
        let para = [
            "pageNum" : pageNumber,
            "pageSize": 30
            ] as [String : Any]
        
        
        MCNetwork.POST(JAVA_getClerkList, para, vc: self, success: { (data) in
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            let dataArray = data.arrayObject ?? [Any]()
            
            if dataArray.count == 0 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            for dict in dataArray {
                let model = MCClerkManageModel()
                model.setValuesForKeys(dict as? [String:Any] ?? [:])
                self.dataArrayM.add(model)
            }
            self.tableView.reloadData()
            
        }, failure: nil)
    }
}

//MARK: UI的处理,通知的接收
extension MCClerkManageViewController {
    func baseSetting() {
        self.title = "店员管理"
        self.view.backgroundColor = UIColor.white
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_image(#imageLiteral(resourceName: "Reuse_add_black"), target: self, selector: #selector(addButtonClicked))
        
        
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, CGFloat(MCSafeAreaBottomHeight), 0))
        }
    }

}




